//引用网络请求
import {
    reqPower,
    reqRole,
    reqDelRole,
    reqDis
} from '../../api/index'
const state = {
    powerList: [],
    roleList: [],
    delthen: [],
    rigthList: []
};

const mutations = {
    GETPOWERLIST(state, powerList) {
        state.powerList = powerList
    },
    GETROLE(state, roleList) {
        state.roleList = roleList
    },
    GETDELTHEN(state, delthen) {
        state.delthen = delthen
    },
    GETDIS(state, rigthList) {
        state.rigthList = rigthList
    }
};

const actions = {
    async getPowerList({
        commit
    }) {
        let res = await reqPower()
        if (res.meta.status == 200) {
            commit('GETPOWERLIST', res.data)
        }
    },
    //角色权限
    async getRole({
        commit
    }) {
        let res = await reqRole()
        if (res.meta.status == 200) {
            commit('GETROLE', res.data)
        }
    },

    //删除权限
    async getDelRole({
        commit
    }, {
        id,
        rightld
    }) {
        let res = await reqDelRole(id, rightld)
        console.log(res);
        if (res.meta.status == 200) {
            commit('GETDELTHEN', res.data)
            return 'ok'
        } else {
            return Promise.reject(new Error('faile'))
        }
    },

    //分配权限
    async getDis({
        commit
    }) {
        let res = await reqDis()
        if (res.meta.status == 200) {
            commit('GETDIS', res.data)
        }
    }
};

const getters = {
    roleList1(state) {
        let arr = [];
        state.roleList.forEach(item => {
            arr.push({
                id: item.id,
                roleName: item.roleName,
                roleDesc: item.roleDesc
            }) || []
        })
        return arr
    }
};

export default {
    state,
    mutations,
    actions,
    getters,
}